package com.demoo.zjsu.service.smartwebsite.zjgsu;

import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.service.smartwebsite.*;
import com.demoo.zjsu.string.smartwebsite.WebsiteStrings;
import com.demoo.zjsu.util.stringutil.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/17.
 * 新闻页面的处理
 */
@Service
public class NewsPageService extends PageListFactoryExtend{


    @Override
    public List<String> getTimes(Elements elements) {
        List<String> strings = new ArrayList<String>();
        for (Element element : elements) {
            String string = element.select(".title_date").text();
            strings.add(string);
        }
        return strings;
    }

    @Override
    public PageTwo dealTitle(String url, String title) {
        return new PageTwo(title, "", url.replace(".html", ""), 0);
    }

    @Override
    public Elements getOtherMain(Document document) {
        Elements elements = document.select("[width=700]");
        int size = elements.select("tr").size();
        if (size == 6) {
            elements.select("tr").eq(3).remove();
            elements.select("tr").eq(0).remove();
        } else if (size == 7) {
            elements.select("tr").eq(4).remove();
            elements.select("tr").eq(1).remove();
            elements.select("tr").eq(0).remove();
        }
        return elements;
    }


    @Override
    public void filterAgain(Elements elements) {
    }

    @Override
    public PageTwoContent getContent(Elements elements) {
        String throat = elements.select("tr").eq(0).text().replaceAll("【.*】", "");
        elements.select("tr").eq(0).remove();
        PageTwoContent content = getPageTwoContent(elements.select("p"));
        content.setThroat(throat);
        return content;
    }

    @Override
    public Page getPage() {
        return new Page(WebsiteStrings.CATEGORY, WebsiteStrings.URI);
    }

    @Override
    public Elements getMain(Document document) {
        return document.select(".titlelist").select("li");
    }

    @Override
    public void filterElse(Elements elements) {

    }


}
